﻿//==============================================================================
// File: LogWindow.xaml.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using WpfRcp.Core.Threading;
using WpfRcp.Views.Presenters;
using StringRes = WpfRcp.Properties.Resources;

namespace WpfRcp.Views.Windows
{
    /// <summary>
    /// Interaction logic for LogWindow. This class is a singleton.
    /// </summary>
    public partial class LogWindow : ILogView
    {
        public LogWindow()
        {
            InitializeComponent();
        }

        #region ILogView Members

        public LogViewPresentationModel PresentationModel
        {
            get { return (LogViewPresentationModel) DataContext; }
            set { DataContext = m_Border.DataContext = value; }
        }

        public object ViewOwner
        {
            set { Owner = (Window) value; }
        }

        #endregion
    }

    public class CancelEnableabilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var operation = value as Job;

            return operation != null && operation.Options.IsCancelable
                   && operation.State != JobState.CancelRequested
                   && operation.State != JobState.Canceled
                   && operation.State != JobState.Completed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }

    public class DateTimeValueConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var dateTime = (DateTime) value;
            return dateTime.ToLocalTime().ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class JobStateValueConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var state = (JobState)value;

            switch (state)
            {
                case JobState.Canceled:
                    return StringRes.JobCancelled;

                case JobState.CancelRequested:
                    return StringRes.JobCancelRequested;

                case JobState.Completed:
                    return StringRes.JobCompleted;

                case JobState.Error:
                    return StringRes.JobError;

                case JobState.Ready:
                    return StringRes.JobReady;

                case JobState.Running:
                    return StringRes.JobRunning;
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}